package com.myapp.struts.menu;
import java.io.*;
import java.util.ArrayList;

import org.w3c.dom.*;

import javax.xml.parsers.*;

import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

public class JavaToXML {

    File f;
    Element MenuItems;
    Element MenuCategory;
    private static JavaToXML xml;

    public JavaToXML getInstance() {
        if (xml == null) {
            xml = new JavaToXML();
            return xml;
        } else {
            return xml;
        }
    }

    public String formatXML(ArrayList<FoodItems> foodItem, String outLoc) {
        try {
            
            f = new File(outLoc, "Menu_XML.xml");
            BufferedWriter bw = new BufferedWriter(new FileWriter(f));

            //Making a document
            DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
            Document doc = docBuilder.newDocument();

            //Creating the XML tree

            //create the root element and add it to the document
            MenuItems = doc.createElement("MenuItems");
            doc.appendChild(MenuItems);

            printXML(foodItem, doc, Constants.BREAKFAST);
            printXML(foodItem, doc, Constants.SNACK);
            printXML(foodItem, doc, Constants.APPETIZER);
            printXML(foodItem, doc, Constants.LUNCH);
            printXML(foodItem, doc, Constants.DINNER_FOOD);
            printXML(foodItem, doc, Constants.DESSERT);
            printXML(foodItem, doc, Constants.SIDE_DISH);
            //add a text element to the child

            //set up a transformer
            TransformerFactory transfac = TransformerFactory.newInstance();
            Transformer trans = transfac.newTransformer();
            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            trans.setOutputProperty(OutputKeys.INDENT, "yes");

            //Make string from xml tree
            StringWriter sw = new StringWriter();
            StreamResult result = new StreamResult(sw);
            DOMSource source = new DOMSource(doc);
            trans.transform(source, result);
            String xmlString = sw.toString();

            //print xml
            bw.write(xmlString);
            bw.close();
            System.out.println("file path " + f.getAbsolutePath());



        } catch (Exception e) {
            System.out.println(e);
        }
        return (f.getAbsolutePath());
    }

    private void printXML(ArrayList<FoodItems> foodItem, Document doc, String type) {
        Boolean head = false;

        for (FoodItems foodItems : foodItem) {
            if (foodItems.getCategory().equals(type)) {
                if (head == false) {
                    MenuCategory = doc.createElement("MenuCategory");
                    MenuCategory.setAttribute("Category", foodItems.getCategory());
                    MenuItems.appendChild(MenuCategory);

                    head = true;
                }
                //creating the child element, and adding the attribute to root the root

                Element MenuItem = doc.createElement("MenuItem");
                MenuCategory.appendChild(MenuItem);

                MenuItem.appendChild(doc.createElement("Name")).appendChild(doc.createTextNode(foodItems.getName()));

                Element price = doc.createElement("Price");
                MenuItem.appendChild(price);

                Element currencyCode = doc.createElement("CurrencyCode");
                Element amount = doc.createElement("Amount");

                if (foodItems.getCountry().equals(Constants.US)) {
                    currencyCode.appendChild(doc.createTextNode("$"));
                } else {
                    currencyCode.appendChild(doc.createTextNode("GBP"));
                }
                amount.appendChild(doc.createTextNode(foodItems.getPrice()));

                price.appendChild(currencyCode);
                price.appendChild(amount);

                MenuItem.appendChild(doc.createElement("Description")).appendChild(doc.createTextNode(foodItems.getDescription()));
            }
        }
    }
}